using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using NHibernate;

namespace TanashCMS.Business
{
    public class ProductOptionType : CMSDataObject
    {
        private TanashWebsite mWebsite = null;
        public virtual TanashWebsite Website { get { return mWebsite; } set { mWebsite = value; } }
        private int mWebsiteID;
        public virtual int WebsiteID { get { return mWebsiteID; } set { mWebsiteID = value; } }

        public static List<ProductOptionType> List(int WebsiteID)
        {
            return (List<ProductOptionType>)Configuration.GetCurrentSession().CreateQuery("select pot from ProductOptionType pot where pot.Website.ID = " + WebsiteID.ToString() + " order by pot.Title").List<ProductOptionType>();
        }

        public static void Update(ProductOptionType optionType)
        {
            ISession session = Configuration.GetCurrentSession();
            ITransaction tx = session.BeginTransaction();

            try
            {
                TanashWebsite website = session.Get<TanashWebsite>(optionType.WebsiteID);
                optionType.Website = website;
                session.SaveOrUpdate(optionType);
                tx.Commit();
                session.Flush();
                session.Disconnect();
            }
            catch (Exception ex)
            {
                tx.Rollback();
                session.Disconnect();
                throw ex;
            }
        }

        public static void Insert(ProductOptionType optionType)
        {
            ISession session = Configuration.GetCurrentSession();
            ITransaction tx = session.BeginTransaction();

            try
            {
                TanashWebsite website = session.Get<TanashWebsite>(optionType.WebsiteID);
                optionType.Website = website;
                session.Save(optionType);
                tx.Commit();
                session.Flush();
                session.Disconnect();
            }
            catch (Exception ex)
            {
                tx.Rollback();
                session.Disconnect();
                throw ex;
            }
        }

        public static void Delete(ProductOptionType optionType)
        {
            ISession session = Configuration.GetCurrentSession();
            ITransaction tx = session.BeginTransaction();

            try
            {
                session.Delete(optionType);
                tx.Commit();
                session.Flush();
                session.Disconnect();
            }
            catch (Exception ex)
            {
                tx.Rollback();
                session.Disconnect();
                throw ex;
            }
        }
    }
}
